﻿using MediatR;
using T_Person.APi.Applation.CommandHandlers.EmployeeHandlers;
using T_Person.BaseRep.Interface;

namespace T_Person.APi.Applation.Commands.EmployeeCommand
{
    public class EmployeeDeleteListCommand : IRequestHandler<EmployeeDeleteList, int>
    {
        private readonly IEmployeeRep employeeRep;

        public EmployeeDeleteListCommand(IEmployeeRep employeeRep)
        {
            this.employeeRep = employeeRep;
        }

        public async Task<int> Handle(EmployeeDeleteList request, CancellationToken cancellationToken)
        {
            try
            {
                var Id = (!string.IsNullOrEmpty(request.ids)) ? request.ids.Split(',').Select(x => Convert.ToInt32(x)).ToArray() : null;
                if (Id != null)
                {
                    var list = await employeeRep.GetvaluesByResult(x => Id.Contains(x.Eid));
                    foreach (var item in list)
                    {
                        item.IsDel = true;
                        await employeeRep.Update(item);
                    }
                  
                }
                return await Task.FromResult(1);
            }
            catch (Exception)
            {
                return await Task.FromResult(-1);
                throw;
            }
           
            
        }
    }
}
